package com.bsoft.exchange.service;


import com.bsoft.exchange.dao.his.QueueCallMapper;
import com.bsoft.exchange.dao.his.SystemConfigMapper;
import com.bsoft.exchange.domain.QueueCallDomain;
import com.bsoft.exchange.util.BaseResponse;
import com.bsoft.exchange.util.Constant;
import com.bsoft.exchange.util.IdentityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.Date;

@Service
public class QueueCallService {
	
	@Autowired
	QueueCallMapper queueCallMapper;
	
	@Autowired
	IdentityManager identityManager ;
	@Autowired
	SystemConfigMapper systemConfigMapper;
	
//	@Autowired
//	DateUtil dateUtil;

//@Transactional(value="hisTransactionManager",rollbackFor = Exception.class)
public BaseResponse<String> createPdjh(int ghxh,int jgid) throws SQLException  {
	BaseResponse<String>baseResponse=new BaseResponse<>();
	//是否启用排队叫号系统
	Long jgidLong = Long.valueOf(jgid);
	int qybz = Integer.valueOf(systemConfigMapper.getValueByConfigName(jgidLong, "PD_QYPDJH")) ;
	if(qybz==0){
		baseResponse.setCode(Constant.successTag);
		baseResponse.setMessage("未启用排队叫号");
		return baseResponse;
	}
	
	QueueCallDomain queueCallDomain = queueCallMapper.getGhxx(ghxh, jgid);
	int brid = queueCallDomain.getBrid();
	String brxm = queueCallDomain.getBrxm();
	String ksid = queueCallDomain.getKsid();
	String ksmc = queueCallDomain.getKsmc();
	String ysid = queueCallDomain.getYsid();
	String ysxm = queueCallDomain.getYsxm();
	int yyxh = queueCallDomain.getYyxh();
	int jzxh = queueCallDomain.getJzxh();
	int zblb = queueCallDomain.getZblb();
	Date yysj = queueCallDomain.getYysj();
	
	int pdzt = 0;
	int fzbz = 0;
	String ywid = String.valueOf(ghxh);
	Integer rdid = queueCallMapper.getRdid(ywid, jgid);
	if(rdid != null && rdid>0){
		pdzt = queueCallMapper.getPdzt(rdid, jgid);
		
		String ztmc;
		switch(pdzt){
		case 1:
			ztmc="等待";
			break;
		case 2:
			ztmc="候诊";
			break;
		case 3:
			ztmc="就诊中";
			break;
		case 4:
			ztmc="完成";
			fzbz = 1;
			break;
		default:
			ztmc="其他";
			break;
		}
		if(pdzt==1||pdzt==2||pdzt==3){
			baseResponse.setCode(Constant.errorTag);
			baseResponse.setData(String.valueOf(rdid));
			baseResponse.setMessage("有队列数据,当前正在"+ztmc);
			return baseResponse;
		}else{
			queueCallMapper.deleteDlb(rdid, jgid);
		}
	}
	
	rdid = queueCallMapper.getRdidLs(ywid, jgid);
	if(rdid != null && rdid>0){
		queueCallMapper.deleteLsdl(rdid, jgid);
		fzbz = 1;
	}

	baseResponse = insertPdjh(jgid,brid,brxm,ksid,ksmc,ysid,ysxm,ghxh,yyxh,jzxh,yysj,zblb,fzbz);
	
    return baseResponse;
	}


	public BaseResponse<String> insertPdjh(int jgid,int brid,String brxm,String ksid,String ksmc,String ysid,String ysxm,int ghxh,int yyxh,int jzxh,Date yysj,int zblb,int fzbz){
		BaseResponse<String>baseResponse=new BaseResponse<>();
		int rdid = (int)identityManager.getMax("PD_DLB", 1);
		Integer dlid = 0;
		int zdys = 0;
		Date kssj=null;
		Date rdsj=null;
		int pdhm = jzxh;
		int sxh = jzxh;
		int pdzt = 1;
		String qtid = String.valueOf(yyxh);
		String qtmc = "01.预约挂号";
		String ywlb = "GH";
		String ywid = String.valueOf(ghxh);
		String brids = String.valueOf(brid);
		
		//优先匹配医生
		if(ysid!=null&&!"".equals(ysid)){
			dlid = queueCallMapper.getYsdlid(ksid, ysid, jgid);
			zdys=1;
		}
		//匹配科室
		if(dlid ==null || dlid==0){
			dlid = queueCallMapper.getKsdlid(Integer.valueOf(ksid) , jgid);
		}
		if(dlid ==null || dlid==0){
			baseResponse.setCode(Constant.errorTag);
			baseResponse.setMessage("未找到对应的队列");
			return baseResponse;
		}
		
		kssj = yysj;
		rdsj = new Date();
		
		
		
		queueCallMapper.insertDlb(jgid, rdid, dlid, rdsj, kssj, pdhm, brxm, pdzt, ksid, ksmc, ysid, ysxm, qtid, qtmc, ywlb, ywid, brids, sxh, 1, fzbz, 0, zblb, zdys);
		
		baseResponse.setCode(Constant.successTag);
		baseResponse.setData(String.valueOf(rdid));
		baseResponse.setMessage("插入队列表成功");
		return baseResponse;
	}

}